package diko_parent.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diko.commonUtils.R;
import diko_parent.eduservice.entity.EduCourse;
import diko_parent.eduservice.entity.vo.CourseInfoForm;
import diko_parent.eduservice.entity.vo.CoursePublishVo;
import diko_parent.eduservice.entity.vo.CourseQuery;
import diko_parent.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Emma-Li
 * @since 2020-09-25
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "新增课程")
    @PostMapping("saveCourse")
    public R saveCource(
            @ApiParam(name="CourseInfoForm",value = "课程基本信息",required = true)
            @RequestBody CourseInfoForm courseInfoForm
            ){
        String courseId = courseService.saveCourceInfo(courseInfoForm);
        if(courseId!="0"){
            return R.ok().data("courseId",courseId);
        }else {
            return R.error().message("保存失败");
        }
    }


    @GetMapping("getCourseInfo/{id}")
    public R getCourseInfo(
            @ApiParam(name="id",value = "课程ID",required = true)
            @PathVariable String id){
        CourseInfoForm courseInfoForm = courseService.getCourseInfo(id);
        return R.ok().data("courseInfoForm",courseInfoForm);
    }

    @PutMapping("updateCourseInfoById/{id}")
    public R updateCourseInfo(
            @ApiParam(name="CourseInfoForm",value = "课程基本信息",required = true)
            @RequestBody CourseInfoForm courseInfoForm,
            @ApiParam(name="id",value = "课程ID",required = true)
            @PathVariable String id
    ){
        courseService.updateCourseInfoById(courseInfoForm);
        return R.ok();
    }


    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("course-publish-info/{id}")
    public R getCoursePublishVoById(
        @ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        CoursePublishVo courseInfoForm = courseService.getCoursePublishVoById(id);
        return R.ok().data("item", courseInfoForm);
    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(
        @ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        courseService.publishCourseById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
        @ApiParam(name = "page", value = "当前页码", required = true)
                @PathVariable Long page,
        @ApiParam(name = "limit", value = "每页记录数", required = true)
                @PathVariable Long limit,
        @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            CourseQuery courseQuery){
            Page<EduCourse> pageParam = new Page<>(page, limit);
            courseService.pageQuery(pageParam, courseQuery);
            List<EduCourse> records = pageParam.getRecords();
            long total = pageParam.getTotal();
            return  R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping("delete/{id}")
    public R deleteCourseById(
            @ApiParam(name="id",value = "课程id",required = true)
            @PathVariable("id") String id
    ){
        courseService.removeCourseById(id);
        return R.ok();
    }
}

